package com.briup.day18;

public class Test_AgeOutRangeException {
    public static void main(String[] args) {
//        Student s1=new Student();
//        s1.setAge(82);
//        System.out.println(s1);
        Student s2=new Student();
        try {
            s2.setAge(82);
        }catch(AgeOutRangeException e) {
            System.out.println("异常捕获");
            e.printStackTrace();
        }
    }
}
/*
* 自定义异常类
* 1.定义异常类
* 2.继承异常父类（编译和运行 父类）
* 3.提供无参构造器
* 4.提供代参构造器（构造方法）*/
class AgeOutRangeException extends RuntimeException {
    public AgeOutRangeException() {

    }
    public AgeOutRangeException(String message) {
        super(message);
    }
}
class Student{
    private int age;
    private String name;
    public Student() {
    }
    public Student(int age, String name) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age < 3 || age > 80) {
            throw new AgeOutRangeException("年龄不在有效范围内");
        }
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}